package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.ServiceLifeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2018/1/30.
 */
@Controller
@RequestMapping(ControllerConstants.SERVICELIFE)
public class ServiceLifeCtronller extends BaseMobileController {

    @Autowired
    private ServiceLifeService serviceLifeService;

    /**
     * 非必选步骤，查询单个话费充值商品：
     * 1.返回指定面值，手机号所在区域下优先级最高商品，优先级："市>省>全国，固定面值>任意充"
     * 2.在同样充值金额下，满足客户选择不同商品需求，可选择与 "查询话费直充商品列表"接口分场景使用
     * 3.可放弃此步骤，直接根据手机号码、充值金额直接生成订单(知道商品编号的前提下)。
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getMobileItemInfo")
    public void getMobileItemInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String mobileNo = getString(json, "mobileNo", "");//手机号
                String rechargeAmount = getString(json, "rechargeAmount", "0");//充值金额
                return serviceLifeService.getMobileItemInfo(mobileNo, rechargeAmount);
            }
        });
    }

    /**
     * 创建充值订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/createdMobileBill")
    public void createdMobileBill(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String mobileNo = getString(json, "mobileNo", "");//账号
                String itemId = getString(json, "itemId", "");//商品编号
                String rechargeAmount = getString(json, "rechargeAmount", "0");//充值金额
                String userName = getString(json, "userName", "");//用户名称
                int payMethod = getInt(json, "payMethod", 1);//支付方式
                int userId = getInt(json, "userId", 0);//充值用户编号
                int isDiscount = getInt(json, "isDiscount", 0);//是否优惠
                Double preferentialAmount = getDouble(json, "preferentialAmount", 0);//优惠金额
                int type = getInt(json, "type", 0);//充值类型     手机话费充值(1),油卡充值(2),水电煤充值(3);
                return serviceLifeService.createdMobileBill(mobileNo, rechargeAmount, payMethod, itemId, userId, isDiscount, preferentialAmount, userName, type);
            }
        });
    }

    /**
     * bm.elife.recharge.mobile.payBill（话费订单充值）
     * 调用此接口，充值话费订单，并且返回订单详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/payMobileBill")
    public void payMobileBill(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int userRechargeId = getInt(json, "userRechargeId", 0);//用户充值下单编号
                return serviceLifeService.payMobileBill(userRechargeId);
            }
        });
    }

    /**
     * bm.elife.directRecharge.waterCoal.item.list（查询水电煤类标准商品列表）
     * 查询上级对接的标准商品列表（信息只包含商品编号与名称），以缩小标准商品选择范围，提高下单成功率。注意：下单时该标准商品是否能正常使用仍限制于上级对于货源的策略控制及货源本身的实时状态。
     * 缴费项目编号，水费c2670，电费c2680，气费c2681；
     *
     * @param request
     * @param response
     */
    @RequestMapping("/waterCoalList")
    public void waterCoalList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int pageNo = getInt(json, "pageNo", 1);//当前页
                int pageSize = getInt(json, "pageSize", 10);//每页条数
                int userId = getInt(json, "userId", 0);//用户编号
                String city = getString(json, "city", "");//城市
                String itemName = getString(json, "itemName", "");//模糊查询的名字
                String projectId = getString(json, "projectId", "");//缴费项目编号
                String province = getString(json, "province", "");//省份  暂时不用
                return serviceLifeService.waterCoalList(pageNo, pageSize, province, city, userId, itemName, projectId);
            }
        });
    }

    /**
     * 基础 bm.elife.directRecharge.waterCoal.item.props.list（查询水电煤标准商品属性列表）
     * 查询指定单一水电煤商品的所有标准属性
     *
     * @param request
     * @param response
     */
    @RequestMapping("/waterCoalItemPropsList")
    public void waterCoalItemPropsList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String itemId = getString(json, "itemId", "");//水电煤标准商品编号，由查询商品接口返回
                return serviceLifeService.waterCoalItemPropsList(itemId);
            }
        });
    }

    /**
     * bm.elife.directRecharge.lifeRecharge.payBill（水电煤订单充值）
     * 水电煤充值订单接口，返回订单详情
     */
    @RequestMapping("/waterCoalPyBill")
    public void waterCoalPyBill(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String itemId = getString(json, "itemId", "");//商品编号 ---机构编号
                String payPrice = getString(json, "payPrice", "");//缴费金额
                int userRechargeId = getInt(json, "userRechargeId", 0);//用户充值下单编号
                int userId = getInt(json, "userId", 0);//用户编号
                int type = getInt(json, "type", 0);//水电煤类型
                String rechargeAccount = getString(json, "rechargeAccount", "");//账号
                return serviceLifeService.waterCoalPyBill(itemId, payPrice, rechargeAccount, userRechargeId, userId, type);
            }
        });
    }

    /**
     * bm.elife.directRecharge.waterCoal.getAccountInfo（查询水电煤欠费账单）
     * 兼容qianmi.elife.directRecharge.lifeRecharge.getAccountInfo接口查询功能
     * itemId	String	必须	612525		标准商品编号(页面选择)
     * account	String	必须	6620519814		缴费单标识号（户号或条形码）
     * projectId	String	必须	c2680		缴费项目编号，水费c2670，电费c2680，气费c2681，(属性查询接口中返回的参数cid)
     * unitId	String	必须	v83589		缴费单位V编号(属性查询接口中返回的参数itemProps-"type": "BRAND"下的vid)
     * province	String	必须	江苏		省名称(后面不带"省"，属性查询接口中返回的参数itemProps-"type": "PRVCIN"下的vname)
     * city	String	必须	南京		市名称(后面不带"市"，属性查询接口中返回的参数itemProps-"type": "CITYIN"下的vname)
     * unitName	String	必须	南京市水费		缴费单位名称(属性查询接口中返回的参数itemProps-"type": "BRAND"下的vname)
     * cityId	String	必须	v2058		城市V编号(属性查询接口中返回的参数itemProps-"type": "CITYIN"下的vid)
     * modeType	String	可选	2		缴费方式：1是条形码 2是户号
     * modeId	String	必须	v2621		缴费方式V编号 (属性查询接口中返回的参数itemProps-"type": "SPECIAL"下的vid)
     */
    @RequestMapping("/getAccountInfo")
    public void getAccountInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String itemId = getString(json, "itemId", "");//商品编号 ---机构编号
                String account = getString(json, "account", "");//缴费单标识号（户号或条形码）
                String projectId = getString(json, "projectId", "");//缴费项目编号，水费c2670，电费c2680，气费c2681，(属性查询接口中返回的参数cid)
                String unitId = getString(json, "unitId", "");//缴费单位V编号(属性查询接口中返回的参数itemProps-"type": "BRAND"下的vid)
                String province = getString(json, "province", "");//省名称(后面不带"省"，属性查询接口中返回的参数itemProps-"type": "PRVCIN"下的vname)
                String city = getString(json, "city", "");//市名称(后面不带"市"，属性查询接口中返回的参数itemProps-"type": "CITYIN"下的vname)
                String unitName = getString(json, "unitName", "");//缴费单位名称(属性查询接口中返回的参数itemProps-"type": "BRAND"下的vname)
                String cityId = getString(json, "cityId", "");//城市V编号(属性查询接口中返回的参数itemProps-"type": "CITYIN"下的vid)
                String modeId = getString(json, "modeId", "");//缴费方式V编号 (属性查询接口中返回的参数itemProps-"type": "SPECIAL"下的vid)
                return serviceLifeService.getAccountInfo(itemId, account, projectId, unitId, province, unitName, cityId, modeId, city);
            }
        });
    }

    /**
     * bm.elife.gasCard.getAccountInfo（查询加油卡的帐号信息）
     * <p>
     * 查询加油卡帐号信息，中石化sinopec，中石油cnpc
     */
    @RequestMapping("/gasCardGetAccountInfo")
    public void gasCardGetAccountInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String province = getString(json, "province", "");//加油卡归属省份名称(不包含"省"，如"江苏","全国")
                String operator = getString(json, "operator", "");//加油卡供应商名称,中石化sinopec,中石油cnpc）
                String gasCardNo = getString(json, "gasCardNo", "");//加油卡卡号
                return serviceLifeService.gasCardGetAccountInfo(province, operator, gasCardNo);
            }
        });
    }

    /**
     * bm.elife.gasCard.gasCardPayBill（加油卡充值）
     * <p>
     * 查询加油卡帐号信息，中石化sinopec，中石油cnpc
     */
    @RequestMapping("/gasCardPayBill")
    public void gasCardPayBill(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String itemId = getString(json, "itemId", "");//	标准商品编号
                String gasCardTel = getString(json, "gasCardTel", "");//持卡人手机号码
                String gasCardName = getString(json, "gasCardName", "");//持卡人完整姓名
                String gasCardNo = getString(json, "gasCardNo", "");//加油卡卡号，中石油为16位，中石化为19位
                String userRechargeId = getString(json, "userRechargeId", "");//用户充值下单编号
                return serviceLifeService.gasCardPayBill(itemId, gasCardTel, gasCardName, gasCardNo, userRechargeId);
            }
        });
    }

    /**
     * bm.elife.gasCard.getAccountInfo（查询加油卡的帐号信息）
     * <p>
     * 查询加油卡帐号信息，中石化sinopec，中石油cnpc
     */
    @RequestMapping("/gasCardList")
    public void gasCardList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {

                return serviceLifeService.gasCardList();
            }
        });
    }

    /**
     * 查询当前账户的缴费记录
     * <p>
     */
    @RequestMapping("/getUserRechargeHistory")
    public void getUserRechargeHistory(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int userId = getInt(json, "userId", 0);//用户编号
                int type = getInt(json, "type", 0);//缴费类型      手机话费充值(1),油卡充值(2),水电煤充值(3);
                int nowPage = getInt(json, "nowPage", 1);//当前页
                return serviceLifeService.getUserRechargeHistory(userId, type, nowPage, 20);
            }
        });
    }

}
